package com.kum.utils;

import com.github.monkeywie.proxyee.util.HttpUtil;
import lombok.extern.log4j.Log4j2;

/**
 * @version V1.0
 * @Package com.kum.utils
 * @auhter 枯木Kum
 * @date 2021/6/19-8:02 PM
 * <p>...</p>
 */

@Log4j2
public class HttpUtils extends HttpUtil {

    public static boolean ruleMatch(String rule, String target) {
        rule = rule.replaceAll(" ", "");
        rule = StringUtils.trim(rule);
        log.info("rule: [{}], targetUrl: [{}]", rule, target);
        int targetCurrentIndex = 0;
        for (int i = 0; i < rule.length(); i++) {
            if (i == rule.length() - 2 &&
                    (rule.charAt(rule.length() - 1) == target.charAt(target.length() - 1) ||
                            rule.charAt(rule.length() - 1) == '*')) {
                return true;
            }
            if (rule.charAt(i) == '*') {
                String temp = target.substring(targetCurrentIndex);
                targetCurrentIndex = temp.indexOf(rule.charAt(i + 1));

                if (targetCurrentIndex == -1) return false;

                targetCurrentIndex = (target.length() - temp.length()) + targetCurrentIndex;

                i++;

            } else if (rule.charAt(i) != target.charAt(targetCurrentIndex)) {
                return false;
            }
            targetCurrentIndex++;
        }
        return false;
    }

    public static void main(String[] args) {
        String url = "http://www.51moot.net/server_hall_2/server_hall_2/video_play?dir_id=4737&amp;do=_do";
        String rule = "*://*.51moot.net/server_hall_2/server_hall_2/*";

        System.out.println(ruleMatch(rule, url));


    }


}
